from apps.account.models import Client, Job, Quote, Expense, ExpenseType, \
    Address, JobCost, Company, Equipment, Brand
from django.contrib import admin

class AddressInline(admin.StackedInline):
    model = Address
    extra = 0
    readonly_fields = ('job',)
    
class ClientAdmin(admin.ModelAdmin):
	inlines = [AddressInline,]
	search_fields = ('first_name', 'last_name', 'phone_telephone', 'phone_mobile',)
	list_display = ('id','title', 'full_name', 'company', 'phone_telephone', 'phone_mobile',)
	
admin.site.register(Client, ClientAdmin)


class JobCostInline(admin.StackedInline):
    model = JobCost
    extra = 0
    
class JobAdmin(admin.ModelAdmin):
	inlines = [AddressInline, JobCostInline]	
	search_fields = ('type', 'description', 'client')
	list_display = ('id', 'date_start', 'client', 'type',  'costs', 'quote_estimate', 'cost_charged', 'profit', 'status''')
	list_filter = ('type', 'date_start', 'client__company__name')
	date_hierarchy = ('date_start')
	ordering = ('-date_start',)
	readonly_fields = ('quote_estimate', 'costs', 'profit')
admin.site.register(Job, JobAdmin)

class QuoteAdmin(admin.ModelAdmin):
	search_fields = ('cost','description', 'client')
admin.site.register(Quote, QuoteAdmin)

class ExpenseAdmin(admin.ModelAdmin):
    search_fields = ('type', 'cost', 'description')
    list_display = ('id', 'date', 'type', 'reciept', 'cost')
    list_filter = ('type', 'date',)
    date_hierarchy = ('date')
    ordering = ('-date',)
    
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == "type":
            kwargs["queryset"] = ExpenseType.objects.all().order_by('type')                                    
        return super(ExpenseAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)    
admin.site.register(Expense, ExpenseAdmin)

admin.site.register(Company)
admin.site.register(ExpenseType)
admin.site.register(Equipment)
admin.site.register(Brand)